package com.xmut.mall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xmut.mall.ware.annotation.CheckJwt;
import com.xmut.mall.ware.to.SkuHasStockVo;
import com.xmut.mall.ware.utils.RespBean;
import com.xmut.mall.ware.vo.EditSkuStockVo;
import com.xmut.mall.ware.vo.OrderEntityVo;
import com.xmut.mall.ware.vo.OrderItemEntityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xmut.mall.ware.entity.WareSkuEntity;
import com.xmut.mall.ware.service.WareSkuService;
import com.xmut.mall.ware.utils.PageUtils;
import com.xmut.mall.ware.utils.R;


/**
 * 商品库存
 *
 * @author 1912120131suntingxing
 * @email 1192081604@qq.com
 * @date 2022-07-14 01:34:47
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    /**
     * 当订单被支付时解锁库存
     * @param param
     * @return
     */
    @PostMapping("/unLockStockWhenOrderPayed")
    public RespBean unLockStockWhenOrderPayed(@RequestBody Map<String, Object> param) {
        return wareSkuService.unLockStockWhenOrderPayed(param);
    }

    /**
     * 解锁库存
     *
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/unLockStock")
    public void unLockStock(@RequestBody Map<String, Object> params) {
        wareSkuService.unLockStock(params);
    }

    /**
     * 保存订单时修改锁库存工作单
     *
     * @param entity
     * @return
     */
    @CheckJwt
    @PostMapping("/updateLockStockTask")
    public RespBean updateLockStockTask(@RequestBody OrderEntityVo entity) {
        return wareSkuService.updateLockStockTask(entity);
    }

    /**
     * 创建锁库存工单
     *
     * @param entity
     * @return
     */
    @CheckJwt
    @PostMapping("/createWareOrderTask")
    public RespBean createWareOrderTask(@RequestBody OrderEntityVo entity) {
        return wareSkuService.createWareOrderTask(entity);
    }

    /**
     * 根据订单项锁商品库存
     *
     * @param entity
     * @return
     */
    @CheckJwt
    @PostMapping("/lockStock")
    public RespBean lockStock(@RequestBody OrderItemEntityVo entity) {
        return wareSkuService.lockStock(entity);
    }

    /**
     * 获取spu的库存情况
     *
     * @param spuId
     * @return
     */
    @CheckJwt
    @GetMapping("/getSpuStock/{spuId}")
    public RespBean getSpuStock(@PathVariable Long spuId) {
        return wareSkuService.getSpuStock(spuId);
    }

    /**
     * 修改sku的库存
     *
     * @param vo
     * @return
     */
    @CheckJwt
    @PutMapping("/editSkuStock")
    public RespBean editSkuStock(@RequestBody EditSkuStockVo vo) {
        return wareSkuService.editSkuStock(vo);
    }

    /**
     * 获取sku的库存详情
     *
     * @param skuId
     * @return
     */
//    @CheckJwt
    @GetMapping("/getSkuWareNumber/{skuId}")
    public RespBean getSkuWareNumber(@PathVariable Long skuId) {
        return wareSkuService.getSkuWareNumber(skuId);
    }

    /**
     * 修改sku的库存
     *
     * @param params
     * @return
     */
    @CheckJwt
    @PutMapping("/editSkuWare")
    public RespBean editSkuWare(@RequestBody Map<String, Object> params) {
        return wareSkuService.editSkuWare(params);
    }

    /**
     * 根据SpuId获取Sku的库存
     *
     * @param spuId
     * @return
     */
    @CheckJwt
    @GetMapping("/getSkuWare/{spuId}")
    public RespBean getSkuWare(@PathVariable Long spuId) {
        return wareSkuService.getSkuWare(spuId);
    }

    /**
     * 添加商品的时候生成库存信息
     *
     * @param list
     * @return
     */
    @CheckJwt
    @PostMapping("/generateSkuWare")
    public RespBean generateSkuWare(@RequestBody List<WareSkuEntity> list) {
        return wareSkuService.generateSkuWare(list);
    }

    // 查询sku是否有库存
    @PostMapping("/hasstock")
    public R getSkusHasStock(@RequestBody List<Long> skuIds) {
        List<SkuHasStockVo> vos = wareSkuService.getSkusHasStock(skuIds);

        return R.ok().setData(vos);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
